"""本文件包含应用程序的身份验证模式。"""

import re
from datetime import datetime

from pydantic import (
    BaseModel,
    Field,
    field_validator,
)


class Token(BaseModel):
    """用于身份验证的令牌模型。

    属性:
        access_token: JWT 访问令牌。
        token_type: 令牌类型（始终为"bearer"）。
        expires_at: 令牌过期时间戳。
    """

    access_token: str = Field(..., description="The JWT access token")
    token_type: str = Field(default="bearer", description="The type of token")
    expires_at: datetime = Field(..., description="The token expiration timestamp")


class TokenResponse(BaseModel):
    """登录端点的响应模型。由于认证由外部系统处理，此模型可能不再需要。

    属性:
        access_token: JWT 访问令牌
        token_type: 令牌类型（始终为"bearer"）
        expires_at: 令牌过期时间
    """

    access_token: str = Field(..., description="The JWT access token")
    token_type: str = Field(default="bearer", description="The type of token")
    expires_at: datetime = Field(..., description="When the token expires")






class SessionResponse(BaseModel):
    """Response model for session creation.

    Attributes:
        session_id: The unique identifier for the chat session
        name: Name of the session (defaults to empty string)
        token: The authentication token for the session
    """

    session_id: str = Field(..., description="The unique identifier for the chat session")
    name: str = Field(default="", description="Name of the session", max_length=100)
    token: Token = Field(..., description="The authentication token for the session")

    @field_validator("name")
    @classmethod
    def sanitize_name(cls, v: str) -> str:
        """Sanitize the session name.

        Args:
            v: The name to sanitize

        Returns:
            str: The sanitized name
        """
        # Remove any potentially harmful characters
        sanitized = re.sub(r'[<>{}[\]()\'"`]', "", v)
        return sanitized
